package com.lin.missyou.api.v1;


import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.lin.missyou.exception.http.NotFoundException;
import com.lin.missyou.models.Category;
import com.lin.missyou.models.GridCategory;
import com.lin.missyou.service.CategoryService;
import com.lin.missyou.service.GridCategoryService;
import com.lin.missyou.vo.CategoriesAllVO;
import com.lin.missyou.vo.CategoryCheckedVO;
import com.lin.missyou.vo.ThemePureVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
@Validated
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private GridCategoryService gridCategoryService;

    @GetMapping("/all")
    public CategoriesAllVO getAll() {
        Map<Integer, List<Category>> categories = this.categoryService.getAll();
        return new CategoriesAllVO(categories);
    }

    @GetMapping("/checked")
    public List<CategoryCheckedVO> getCategoriesChecked() {
        List<Category> categories = this.categoryService.getCategoriesChecked();
        List<CategoryCheckedVO> categoryCheckedVOList = new ArrayList<>();
        categories.forEach(item->{
            Mapper mapper = DozerBeanMapperBuilder.buildDefault();
            CategoryCheckedVO vo = mapper.map(item, CategoryCheckedVO.class);
            categoryCheckedVOList.add(vo);
        });
        return categoryCheckedVOList;
    }

    @GetMapping("/grid/all")
    public List<GridCategory> getAllGridCategory() {
        List<GridCategory> gridCategories = this.gridCategoryService.getGridCategoryList();
        if(gridCategories.isEmpty()){
            throw new NotFoundException(30009);
        }
        return gridCategories;
    }
}
